package com.ts.code.creator.express;

public class Express {
    private String express;

    public Express(String express) {
        this.express = express;
    }

    public String eval(ExpressContext context) {
        char[] chars = express.toCharArray();

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == '$' && chars[i + 1] == '{') {

                int j = i + 1;
                for (; j < chars.length; j++) {
                    if (chars[j] == '}') {
                        break;
                    }
                }

                String var = new String(chars, i + 2, j - i - 2);
                Object str = context.getObject(var);
                if (str != null) {
                    sb.append(str);
                }
                i = j;

            } else {
                sb.append(chars[i]);
            }
        }

        return sb.toString();
    }

    public static void main(String[] args) {
        Express express = new Express("asdfkasdf ${aa} asdlkfj");
        ExpressContext context = new ExpressContext();
        context.put("aa", "hello");
        System.out.println(express.eval(context));
    }
}
